package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.CmorderApplyDto;
import com.dingreading.cloud.soms.dto.CmorderDto;
import com.dingreading.cloud.soms.entity.CmorderApplyImg;
import com.dingreading.cloud.soms.entity.CmorderApplyLog;
import com.dingreading.cloud.soms.service.CmorderApplyImgService;
import com.dingreading.cloud.soms.service.CmorderApplyLogService;
import com.dingreading.cloud.soms.service.CmorderApplyService;
import com.dingreading.cloud.soms.service.CmordersService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/front/cmOrderApply")
@Api(tags = "门店-会员服务订单申请管理")
public class FrontCmOrderApplyController {

    @Resource
    protected CmorderApplyService cmorderApplyService;
    @Resource
    protected CmorderApplyLogService cmorderApplyLogService;
    @Resource
    protected CmorderApplyImgService cmorderApplyImgService;
    @Resource
    protected CmordersService cmordersService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "申请类型 1.变更 2.退费") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(value = "关键词") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "收款开始日期") @RequestParam(value = "beginDate", required = false) String beginDate,
            @ApiParam(value = "收款结束日期") @RequestParam(value = "endDate", required = false) String endDate
    ) {
        Page<CmorderApplyDto> page = cmorderApplyService.pageList(pageUtil, corpUid, storeUid, type, beginDate, endDate, keyword);

        List<CmorderApplyDto> list = page.getRecords();
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> applyCodeList = list.stream().map(CmorderApplyDto::getApplyCode).collect(Collectors.toList());

            List<CmorderApplyImg> imgList = cmorderApplyImgService.listByApplyCodeIn(applyCodeList);
            Map<String, List<CmorderApplyImg>> imgMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(imgList)) {
                imgMap = imgList.stream().collect(Collectors.groupingBy(CmorderApplyImg::getApplyCode));
            }

            List<CmorderApplyLog> logList = cmorderApplyLogService.listByApplyCodeIn(applyCodeList);
            Map<String, List<CmorderDto>> applyLogMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(logList)) {
                List<String> orderCodeList = logList.stream().map(CmorderApplyLog::getOrderCode).collect(Collectors.toList());
                Map<String, List<CmorderApplyLog>> logMap = logList.stream().collect(Collectors.groupingBy(CmorderApplyLog::getApplyCode));
                List<CmorderDto> cmorderDtos = cmordersService.listByOrderCodeInd(orderCodeList);
                Map<String, CmorderDto> cmorderDtoMap = new HashMap<>();
                if (CollectionUtils.isNotEmpty(cmorderDtos))
                    cmorderDtoMap = cmorderDtos.stream().collect(Collectors.toMap(CmorderDto::getOrderCode, Function.identity()));

                for (Map.Entry<String, List<CmorderApplyLog>> entry : logMap.entrySet()) {
                    String applyCode = entry.getKey();
                    List<CmorderApplyLog> applyLogs = entry.getValue();

                    List<CmorderDto> dtoList = new ArrayList<>();
                    for (CmorderApplyLog applyLog : applyLogs) {
                        CmorderDto dto = cmorderDtoMap.get(applyLog.getOrderCode());
                        if (dto != null)
                            dtoList.add(dto);
                    }
                    applyLogMap.put(applyCode, dtoList);
                }
            }

            for (CmorderApplyDto apply : list) {
                apply.setImgList(imgMap.get(apply.getApplyCode()));
                apply.setLogList(applyLogMap.get(apply.getApplyCode()));
            }
        }
        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(list);
        return R.ok(data);
    }


}
